from deepdiff import DeepDiff

# 定义两个复杂的字典对象进行比较
obj1 = {
    "name": "Alice",
    "age": 30,
    "address": {
        "city": "New York",
        "zip": "10001"
    },
    "hobbies": ["reading", "swimming", "coding"],
    "scores": [95, 88, 92],
    "metadata": {
        "created_at": "2023-01-01",
        "updated_at": "2023-01-10"
    }
}

obj2 = {
    "name": "Bob",  # 姓名变更
    "age": 30,
    "address": {
        "city": "Los Angeles",  # 城市变更
        "zip": "90001"  # 邮编变更
    },
    "hobbies": ["reading", "hiking", "coding"],  # 兴趣爱好变更
    "scores": [95, 92, 92],  # 分数位置变更
    "metadata": {
        "created_at": "2023-01-01",
        # updated_at 字段被删除
        "extra_info": "new field"  # 新增字段
    }
}

# 使用 DeepDiff 比较两个对象
diff = DeepDiff(obj1, obj2, ignore_order=False,
                exclude_paths=["root['metadata']['updated_at']"]
                )

# 输出比较结果
print("### 对象差异分析 ###")
print("\n1. 值变更:")
if 'values_changed' in diff:
    for key, value in diff['values_changed'].items():
        print(f"  - {key}:")
        print(f"    旧值: {value['old_value']}")
        print(f"    新值: {value['new_value']}")
else:
    print("  无值变更")

print("\n2. 新增项:")
if 'dictionary_item_added' in diff:
    for item in diff['dictionary_item_added']:
        print(f"  - {item}")
else:
    print("  无新增项")

print("\n3. 删除项:")
if 'dictionary_item_removed' in diff:
    for item in diff['dictionary_item_removed']:
        print(f"  - {item}")
else:
    print("  无删除项")

print("\n4. 列表顺序变更:")
if 'iterable_item_moved' in diff:
    for key, value in diff['iterable_item_moved'].items():
        print(f"  - {key}:")
        print(f"    从位置 {value['from_index']} 移动到 {value['to_index']}")
else:
    print("  无列表顺序变更")

print("\n5. 类型变更:")
if 'type_changes' in diff:
    for key, value in diff['type_changes'].items():
        print(f"  - {key}:")
        print(f"    旧类型: {type(value['old_value']).__name__}")
        print(f"    新类型: {type(value['new_value']).__name__}")
else:
    print("  无类型变更")
